package cn.apestar.modules.image.web;

import cn.apestar.common.config.Global;
import cn.apestar.common.exception.BaseException;
import cn.apestar.common.image.FileUtilsIo;
import cn.apestar.common.web.BaseController;
import cn.apestar.modules.image.service.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 文档管理
 * 
 * @author lvh
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/file")
public class ImageController extends BaseController {

    @Autowired
    private ImageService imageService;

    /**
     * 图片上传
     * 
     * @param file
     * @return
     */
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public String deploy(MultipartHttpServletRequest request, HttpServletResponse response, Model redirectAttributes) {

        Map map = new HashMap();
        try {
            map = imageService.deployQN(request, response);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            redirectAttributes.addAttribute("success", "2");
            if (e instanceof BaseException) {
                redirectAttributes.addAttribute("flag", e.getMessage());
                return "modules/comp/upload";
            } else {
                redirectAttributes.addAttribute("flag", "3");
                return "modules/comp/upload";
            }
        }
        redirectAttributes.addAttribute("success", "1");
        redirectAttributes.addAttribute("a", map.get("a"));
        redirectAttributes.addAttribute("spath", map.get("spath"));
        redirectAttributes.addAttribute("c_pname", map.get("name"));
        return "modules/comp/upload";
    }

    /**
     * 图片上传跳转
     * 
     * @param file
     * @return
     */
    @RequestMapping(value = "uploadlist", method = RequestMethod.GET)
    public String uploadlist(Model redirectAttributes, HttpServletRequest request) {

        String a = request.getParameter("a");
        redirectAttributes.addAttribute("success", "0");
        redirectAttributes.addAttribute("a", a);
        return "modules/comp/upload";
    }

    /**
     * 图片上传跳转
     * 
     * @param file
     * @return
     */
    @RequestMapping(value = "uploadlistFile", method = RequestMethod.GET)
    public String uploadlistFile(Model redirectAttributes, HttpServletRequest request) {

        String a = request.getParameter("a");
        redirectAttributes.addAttribute("success", "0");
        redirectAttributes.addAttribute("a", a);
        return "modules/comp/uploadFile";
    }

    /**
     * 图片上传跳转
     * 
     * @param file
     * @return
     */
    @RequestMapping(value = "uploadlistTest", method = RequestMethod.GET)
    public String uploadlistTest(Model redirectAttributes) {

        redirectAttributes.addAttribute("success", "0");
        return "modules/comp/uploadlistTest";
    }

    /**
     * 图片上传跳转
     * 
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "getImage", method = RequestMethod.GET)
    public void getImage(String path, String image, HttpServletResponse response) throws IOException {

        String month = image.substring(0, 6);
        String ri = image.substring(6, 8);
        File file = new File(
            Global.getProjectImagePath() + "pic\\" + month + "\\" + ri + "\\" + "thumb_" + image);
        OutputStream output = null;
        response.setHeader("Content-Disposition", "inline; filename=" + image);// 下载
        response.setContentType(FileUtilsIo.getContentType(image));
        try {
            output = response.getOutputStream();
            FileUtilsIo.outputFile(file, output);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            output.close();
        }
    }

}
